#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QFont>
#include <QPixmap>
#include "button.h"

Button::Button(QString strw, QString struri, QString tips, QPixmap p,QToolButton*parent) :
    QToolButton(parent)
{
    text = strw;
    bg_uri = struri;
    p_wh = p;
    is_release = false;
    str_tips = tips;
    this->setMouseTracking(true);
}

Button::~Button()
{
}

void Button::mousePressEvent(QMouseEvent *e)
{
    e;
    QPixmap p;
    QString s = bg_uri;
    p.load(s.append("_pressed.png"));
    this->setIcon(p);
    emit clicked();
}

void Button::mouseReleaseEvent(QMouseEvent *e)
{
    e;
    QPixmap p;
    QString s = bg_uri;
    p.load(s.append("_sel.png"));
    this->setIcon(p);
    is_release = true;
}

void Button::paintEvent(QPaintEvent *event)
{
    event;
    QToolButton::paintEvent(event) ;
    QPainter painter(this) ;
    painter.drawText(QPoint(p_wh.width()/3,p_wh.height()/1.5),text);
    return ;
}

void Button::enterEvent(QEvent *e)//鼠标进入事件
{
    e;
    this->setToolTip(str_tips);
    if(!is_release)
    {
        QPixmap p;
        QString s = bg_uri;
        p.load(s.append("_prelight.png"));
        this->setIcon(p);
    }
}

void Button::leaveEvent(QEvent *e)//离开
{
    e;
    if(!is_release)
    {
        QPixmap p;
        QString s = bg_uri;
        p.load(s.append("_normal.png"));
        this->setIcon(p);
    }
}
